package com.yunsc.order.handler.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunsc.order.entity.YunscOrderEntity;
import com.yunsc.order.entity.YunscOrderGoodsEntity;
import com.yunsc.order.handler.RedisDelayQueueHandle;
import com.yunsc.order.handler.exception.DelayExceptionHandler;
import com.yunsc.order.sevice.YunscOrderGoodsService;
import com.yunsc.order.sevice.YunscOrderService;
import com.yunsc.product.entity.YunscCommentEntity;
import com.yunsc.product.service.YunscCommentService;
import com.yunsc.store.service.YunscStoreSettingService;
import com.yunsc.store.vo.OrderSettingVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @version: 1.0
 * @Author: adan
 * @description: 订单超时未评价处理类
 * @date: 2024-05-25 15:45
 */
@Component("orderTimeoutNotEvaluated")
@Slf4j
public class OrderTimeoutNotEvaluated implements RedisDelayQueueHandle<Map> {

    @Resource
    private YunscOrderService orderService;
    @Resource
    private YunscOrderGoodsService orderGoodsService;
    @Resource
    private YunscCommentService commentService;
    @Resource
    private YunscStoreSettingService storeSettingService;

    @Override
    @Transactional
    @DelayExceptionHandler
    public void execute(Map map) {
        log.info("(收到订单超时未评价延迟消息) {}", map);
        Integer orderId = (Integer) map.get("orderId");
        YunscOrderEntity orderEntity = orderService.getOne(new LambdaQueryWrapper<YunscOrderEntity>()
                .eq(YunscOrderEntity::getOrderId, orderId).eq(YunscOrderEntity::getIsDelete, 0));
        if (orderEntity == null) {
            return;
        }
        if (orderEntity.getIsComment() == 1) {
            return;
        }

        // 订单超时未评价处理逻辑
        LambdaQueryWrapper<YunscOrderGoodsEntity> queryWrapper = new LambdaQueryWrapper<YunscOrderGoodsEntity>()
                .eq(YunscOrderGoodsEntity::getOrderId, orderId)
                .eq(YunscOrderGoodsEntity::getIsComment, 0);
        List<YunscOrderGoodsEntity> orderGoodsList = orderGoodsService.list(queryWrapper);
        // 自定义评价内容
        OrderSettingVO orderSettingVO = storeSettingService.queryOrderSettingVO(orderEntity.getStoreId());
        String evaluateContent = orderSettingVO.getOrder().getEvaluateContent();

        List<YunscOrderGoodsEntity> orderGoodsEntityList = new ArrayList<>();
        List<YunscCommentEntity> commentList = new ArrayList<>();
        orderGoodsList.forEach(orderGoods -> {
            YunscCommentEntity comment = new YunscCommentEntity();
            comment.setScore(10);
            comment.setContent(evaluateContent);
            comment.setOrderId(orderGoods.getOrderId());
            comment.setOrderGoodsId(orderGoods.getOrderGoodsId());
            comment.setGoodsId(orderGoods.getGoodsId());
            comment.setUserId(orderEntity.getUserId());
            comment.setStoreId(orderEntity.getStoreId());
            commentList.add(comment);

            // 更新订单商品状态
            orderGoods.setIsComment(1);
            orderGoodsEntityList.add(orderGoods);

        });

        commentService.saveBatch(commentList);


        // 更新订单状态
        orderEntity.setIsComment(1);
        orderService.updateById(orderEntity);
        // 更新订单商品状态
        orderGoodsService.updateBatchById(orderGoodsEntityList);

        log.info("(订单超时未评价处理完成) {}", map);
    }
}
